package com.ljkj.cordial.chat.adapter;

import android.graphics.BitmapFactory;
import android.graphics.PorterDuff;
import android.support.annotation.NonNull;
import android.support.v4.content.ContextCompat;
import android.support.v4.widget.ContentLoadingProgressBar;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.ljkj.cordial.chat.R;
import com.ljkj.cordial.chat.bean.Canstants;
import com.ljkj.cordial.chat.dao.AlbumTableBean;
import com.ljkj.cordial.chat.dao.ChatTableBean;
import com.ljkj.cordial.chat.util.AlbumManageUtil;
import com.nic.view.util.glide.GlideUtil;
import com.nic.view.widget.ZoomImageView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class PhotoHorizontalAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private List<ChatTableBean> list = new ArrayList<>();

    public void setList(List<ChatTableBean> list) {
        if (list==null||list.isEmpty()) {
            return;
        }
        this.list = list;
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_photo_horizontal, null);
        view.setLayoutParams(params);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int i) {
        ViewHolder cusHolder = (ViewHolder) viewHolder;
        ChatTableBean item = list.get(i);
        cusHolder.ivImage.setImageBitmap(BitmapFactory.decodeFile(item.getThumbPath()));
//        GlideUtil.getInstance().load(item.getThumbPath(), cusHolder.ivImage);
        cusHolder.ivImage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                cusHolder.progressBar.show();
                if (Canstants.SINGLE_CHAT == item.getFromType())
                    AlbumManageUtil.getInstance().downLoadFileFriend(item.getFileUrl(), AlbumTableBean.TYPE_IMAGE, item.getTargetId(), new AlbumManageUtil.OnFileResponse() {
                        @Override
                        public void onResult(String url, String fileId) {
                            resultFile(fileId, item, cusHolder.ivImage, cusHolder.progressBar);
                        }
                    });
                else
                    AlbumManageUtil.getInstance().downLoadFileGroupMember(item.getFileUrl(), AlbumTableBean.TYPE_IMAGE
                            , item.getGroupType(), item.getGroupId(), item.getTargetType(), item.getTargetId(), new AlbumManageUtil.OnFileResponse() {
                                @Override
                                public void onResult(String url, String fileId) {
                                    resultFile(fileId, item, cusHolder.ivImage, cusHolder.progressBar);
                                }
                            });
            }
        });
    }

    private void resultFile(String fileId, ChatTableBean item, ZoomImageView zoomImageView, ContentLoadingProgressBar progressBar) {
        progressBar.hide();
        if (!TextUtils.isEmpty(fileId) && Long.parseLong(fileId) > 0) {
            item.setAlbumId(fileId);
            ChatTableBean.update(item);
            AlbumTableBean al = AlbumTableBean.queryById(Long.parseLong(fileId));
            if (al != null && !TextUtils.isEmpty(al.getMPath())) {
//                GlideUtil.getInstance().load(al.getMPath(), zoomImageView);
                zoomImageView.setImageBitmap(BitmapFactory.decodeFile(al.getMPath()));
            }
        } else {
            zoomImageView.setImageBitmap(BitmapFactory.decodeFile(item.getThumbPath()));
//            GlideUtil.getInstance().load(item.getThumbPath(), zoomImageView);
        }
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.image)
        ZoomImageView ivImage;
        @BindView(R.id.item_progress)
        ContentLoadingProgressBar progressBar;

        ViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
            progressBar.getIndeterminateDrawable().setColorFilter(ContextCompat.getColor(view.getContext(), R.color.blue3), PorterDuff.Mode.MULTIPLY);
            progressBar.hide();
        }
    }
}
